package com.ruoyi.bill.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.baseMain.domain.vo.BaseAccountVo;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.dataMain.domain.vo.DataConsumptionVo;
import com.ruoyi.dataMain.listener.BaseAccountImportListener;
import com.ruoyi.dataMain.listener.BillBankPortImportListener;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.bill.domain.vo.BillBankPortVo;
import com.ruoyi.bill.domain.bo.BillBankPortBo;
import com.ruoyi.bill.service.IBillBankPortService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 银行端口流水
 *
 * @author linziheng
 * @date 2024-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bill/bankPort")
public class BillBankPortController extends BaseController {

    private final IBillBankPortService iBillBankPortService;

    /**
     * 查询银行端口流水列表
     */
    @SaCheckPermission("bill:bankPort:list")
    @GetMapping("/list")
    public TableDataInfo<BillBankPortVo> list(BillBankPortBo bo, PageQuery pageQuery) {
        return iBillBankPortService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出银行端口流水列表
     */
    @SaCheckPermission("bill:bankPort:export")
    @Log(title = "银行端口流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BillBankPortBo bo, HttpServletResponse response) {
        List<BillBankPortVo> list = iBillBankPortService.queryList(bo);
        ExcelUtil.exportExcel(list, "银行端口流水", BillBankPortVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "端口流水导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BillBankPortVo> result = ExcelUtil.importExcel(file.getInputStream(), BillBankPortVo.class, new BillBankPortImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "消耗总表", DataConsumptionVo.class, response);
    }
    /**
     * 获取银行端口流水详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("bill:bankPort:query")
    @GetMapping("/{id}")
    public R<BillBankPortVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBillBankPortService.queryById(id));
    }

    /**
     * 新增银行端口流水
     */
    @SaCheckPermission("bill:bankPort:add")
    @Log(title = "银行端口流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BillBankPortBo bo) {
        return toAjax(iBillBankPortService.insertByBo(bo));
    }

    /**
     * 修改银行端口流水
     */
    @SaCheckPermission("bill:bankPort:edit")
    @Log(title = "银行端口流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BillBankPortBo bo) {
        return toAjax(iBillBankPortService.updateByBo(bo));
    }

    /**
     * 删除银行端口流水
     *
     * @param ids 主键串
     */
    @SaCheckPermission("bill:bankPort:remove")
    @Log(title = "银行端口流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBillBankPortService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
